﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ResxHelper.Logic;
using ResxHelper.Logic.Converter;

namespace WebInterface.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult HandleFileUpload()
        {
            var header = Request.Headers["X-File-Name"];
            var filePath = string.Empty;
            if (!string.IsNullOrEmpty(header))
            {
                filePath = Server.MapPath(string.Format("~/Uploads/{0}", Request.Headers["X-File-Name"]));
                var inputStream = Request.InputStream;

                var fileStream = new FileStream(filePath, FileMode.OpenOrCreate);

                inputStream.CopyTo(fileStream);
                fileStream.Close();
            }

            // HANDLE IT!
            var savePath = Path.GetDirectoryName(filePath) + "\\output.zip";
            var converter = BulkConverterFactory.GetConverter(Core.ZipResourceContent.Android);
            converter.ToExcel(filePath, savePath);

            var fileResult = new FileStreamResult(new FileStream(savePath, FileMode.Open),
                System.Net.Mime.MediaTypeNames.Application.Zip);
            fileResult.FileDownloadName = "output_excel.zip";

            return fileResult;
        }
    }
}
